//
//  BTSearchView.m
//  BreadTrip
//
//  Created by Vivitickey on 2017/4/19.
//  Copyright © 2017年 test. All rights reserved.
//

#import "BTSearchView.h"
#import "UIColor+SWAddition.h"
#import <Masonry.h>
#import "BTWebService.h"
#import "BTSearchModel.h"

@interface BTSearchView () <UIScrollViewDelegate>

@property (nonatomic,strong) BTSearchModel *model;

@end

@implementation BTSearchView



-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        
        [self loadData];
    }
    
    return self;
    
}


-(void)setupUI{
    
    self.backgroundColor = [UIColor redColor];
    
    self.backgroundColor = [UIColor sw_colorWithR:246 G:244 B:231];
    
    UILabel *overseasLabel = [[UILabel alloc]init];
    
    
    //国内热门 label
    overseasLabel.text = @"国外热门目的地";
    
    overseasLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(16.0)];
    
    [self addSubview:overseasLabel];
    
    [overseasLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self).offset(160);
        
        make.centerX.equalTo(self);
        
    }];
    
    //伪代码
    NSArray *overseasArr = @[@"奈良市",@"小樽市",@"槟城",@"墨尔本",@"广岛县",@"泰国",@"芬兰",@"美国",@"马来西亚",@"意大利",@"新加坡",@"挪威"];
    //国外热门 button
    UIView *overseasView = [self addButtonWithCityArray:overseasArr];
    
    [self addSubview:overseasView];
    
    [overseasView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(overseasLabel.mas_bottom).offset(20);
        
        make.width.equalTo(self);
        
        make.height.mas_offset(overseasView.frame.size.height);
        
    }];
    
    //国内热门 label
    UILabel *domesticLabel = [[UILabel alloc]init];
    
    domesticLabel.text = @"国内热门目的地";
    
    domesticLabel.font = [UIFont fontWithName:@ "Arial Rounded MT Bold"  size:(16.0)];
    
    [self addSubview:domesticLabel];
    
    [domesticLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(overseasView.mas_bottom).offset(50);
        
        make.centerX.equalTo(self);
        
    }];
   
    //伪代码
    NSArray *domesticArr =@[@"台湾",@"香港",@"厦门",@"北京",@"丽江市",@"成都",@"上海",@"拉萨",@"大理",@"三亚"];
    
    //国内热门 button
    UIView * domesticView = [self addButtonWithCityArray:domesticArr];
    
    
    [self addSubview:domesticView];
    
    [domesticView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(domesticLabel.mas_bottom).offset(20);
        
        make.width.equalTo(self);
        
        make.height.mas_offset(domesticView.frame.size.height);
        
    }];
    
    
    
    //设置scrollView 滚动
    [self setContentSize:CGSizeMake(self.frame.size.width, self.frame.size.height - 63)];
    //隐藏滚动条
    self.showsVerticalScrollIndicator = NO;
   
}

#pragma mark - 网络请求 -
-(void)loadData{
    
    NSString *urlString = Search_url;
        //sessionid=9cc3738a79df2c6b3c49c9614098ebe4; bt_devid=i_3b3ab7b98d82994dcb39634cf7a1b5d6e03123adb4f021e60810b052ce8716c0dad6ece4; Hm_lvt_39ccc5353de4e1969b414e80e516d5a7=1492519019,1492519982,1492661419,1492946245; gr_user_id=16cb4e86-29b1-46c7-ae5c-a52614515c8d
    
    
//    NSDictionary *paramdic = @{@"sessionid":@"9cc3738a79df2c6b3c49c9614098ebe4",@"bt_devid":@"i_3b3ab7b98d82994dcb39634cf7a1b5d6e03123adb4f021e60810b052ce8716c0dad6ece4",@"Hm_lvt_39ccc5353de4e1969b414e80e516d5a7":@"1492519019,1492519982,1492661419,1492946245",@"gr_user_id":@"16cb4e86-29b1-46c7-ae5c-a52614515c8d"
//                               };
    
//    NSDictionary *paramdic = @{@"session_id":@"9cc3738a79df2c6b3c49c9614098ebe4"};
    
    
//    [[BTWebService shareNetworkTool] POSTWithUrlString:urlString parameters:paramdic success:^(id responseObject) {
//        
//        NSDictionary *dict = responseObject[@"data"][@"search_data"][@"elements"];
//        
//        self.model.name = dict[@"name"];
//        
//        self.model.url = dict[@"url"];
//        
//        //self.model.lat = dict[@"elements"];
//        
//        [self setupUI];
//        
//    } failure:^(NSError *error) {
//        NSLog(@"%@",error);
//    }];
    
    [[BTWebService shareNetworkTool] GETWithUrlString:urlString parameters:nil success:^(id responseObject) {
        NSDictionary *dict = responseObject[@"data"];
                self.model.name = dict[@"name"];
        
                self.model.url = dict[@"url"];
        
                //self.model.lat = dict[@"elements"];
        
        self.model.lat = 0;
        
        self.model.lng = 0;
                
                [self setupUI];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}


#pragma mark - button创建方法 -

//循环创建Button
-(UIView *)addButtonWithCityArray:(NSArray *)arr{
    
    
    UIView *SearchButtonView = [[UIView alloc]init];
    
    
    CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离
    
    CGFloat h = 0;
    
    int tag = 0;
    
    UIView *buttonView1 = [[UIView alloc]init];
    UIView *buttonView2 = [[UIView alloc]init];
    UIView *buttonView3 = [[UIView alloc]init];
    
    for (int i = 0; i < arr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        
        button.backgroundColor = [UIColor clearColor];
        
        //设置圆角
        button.layer.cornerRadius = 15;//2.0是圆角的弧度，根据需求自己更改
        button.layer.borderColor =[UIColor lightGrayColor].CGColor;//设置边框颜色
        button.layer.borderWidth = 0.5f;//设置边框颜色
        
        [button addTarget:self action:@selector(handleClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        
        //根据计算文字的大小
        
        
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
        CGFloat length = [arr[i] boundingRectWithSize:CGSizeMake(self.frame.size.width, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        //为button赋值
        [button setTitle:arr[i] forState:UIControlStateNormal];
        
        //当button的位置超出屏幕边缘时换行  只是button所在父视图的宽度
        if(10 + w + length + 30 > self.frame.size.width){
            w = 0; //换行时将w置为0
            h = h + 40;//距离父视图也变化
            tag ++;
            button.frame = CGRectMake(10 + w, 0, length + 35, 30);//重设button的frame
        }else{
            
            
            //设置button的frame
            button.frame = CGRectMake(10 + w, 0, length + 35 , 30);
            
        }
        w = button.frame.size.width + button.frame.origin.x;
        
        
        switch (tag) {
            case 0:
                [buttonView1 addSubview:button];
                
                buttonView1.frame = CGRectMake((self.frame.size.width - w -length + 35)/2 , h, w + length + 35, 30);
                
                break;
            case 1:
                [buttonView2 addSubview:button];
                
                buttonView2.frame = CGRectMake((self.frame.size.width - w -length + 35)/2 , h, w + length + 35, 30);
                
                break;
            case 2:
                [buttonView3 addSubview:button];
                
                buttonView3.frame = CGRectMake((self.frame.size.width - w -length + 35)/2 , h, w + length + 35, 30);
                
            default:
                break;
        }
        
    }
    
    
    
    [SearchButtonView addSubview:buttonView1];
    
    [SearchButtonView addSubview:buttonView2];
    
    [SearchButtonView addSubview:buttonView3];
    
    SearchButtonView.frame = CGRectMake(0, 0, self.frame.size.width, h + 30);
    
    return SearchButtonView;
    
}

- (void)handleClick:(UIButton *)btn{
    NSLog(@"%ld",btn.tag);
}

@end
